package com.tinyengine.it.controller.db;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tinyengine.it.common.base.Result;
import com.tinyengine.it.model.entity.DbTableFieldsInfo;
import com.tinyengine.it.model.vo.DbTableFieldsInfoVo;
import com.tinyengine.it.service.db.DbTableFieldsInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/table-field/api")
@Tag(name = "数据库表字段维护")
@Validated
@RequiredArgsConstructor
public class DbTableFieldsInfoController {

    private final DbTableFieldsInfoService dbTableFieldsInfoService;

    @Operation(summary = "跟表ID获取字段列表",
            parameters = {
                    @Parameter(name = "tableId", description = "表ID")
            },
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = DbTableFieldsInfo.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    @GetMapping("/getFieldsInfoByTableId")
    public Result<List<DbTableFieldsInfoVo>> getFieldsInfoByTableId(@RequestParam("tableId") Integer tableId) {
        List<DbTableFieldsInfo> list = dbTableFieldsInfoService.list(Wrappers.<DbTableFieldsInfo>lambdaQuery().eq(DbTableFieldsInfo::getTableId, tableId));
        return Result.success(BeanUtil.copyToList(list, DbTableFieldsInfoVo.class));
    }

    @Operation(summary = "新增及更新",
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = Result.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    @PostMapping("/addAndUpdate")
    public Result addAndUpdate(@RequestBody @Validated List<DbTableFieldsInfoVo> dbTableFieldsInfoVos) {
        List<DbTableFieldsInfo> dbTableFieldsInfo = BeanUtil.copyToList(dbTableFieldsInfoVos, DbTableFieldsInfo.class);
        dbTableFieldsInfoService.saveOrUpdateBatch(dbTableFieldsInfo);
        return Result.success();
    }


    @Operation(summary = "根据ID删除",
            parameters = {
                    @Parameter(name = "id", description = "ID")},
            responses = {@ApiResponse(responseCode = "200", description = "返回信息",
                    content = @Content(mediaType = "application/json",
                            schema = @Schema(implementation = Result.class))),
                    @ApiResponse(responseCode = "400", description = "请求失败")}
    )
    @GetMapping("/deleteById/{id}")
    public Result deleteProjectServiceGroupInf1o(@Valid @PathVariable Integer id) {
        dbTableFieldsInfoService.removeById(id);
        return Result.success();
    }
}
